import Vue from 'vue';
import { bucket_name, libraryFolderName } from '@/config';
import functions from '@/router/modules/functions';

const prefix = '/userProject/';

/**
 * 获取用户全部函数库
 * request: cookie //为分页做准备
 */
export const getFunctionLibrary = () => {
  return Vue.$http({
    url: prefix + 'getFunctionLibrary',
    method: 'get'
  });
};

/**
 * 新建函数库
 * request: {
 *  "libraryName":"", //函数库名
 *  "desc":"", //说明
 * }
 */
export const createFunctionLibrary = (data) => {
  return Vue.$http({
    url: prefix + 'createFunctionLibrary',
    method: 'post',
    data
  });
};

/** TODO 删除的时候删除模版文件
 * 删除函数库
 * request: libraryName:"" //函数库名
 */
export const deleteFunctionLibrary = (params) => {
  return Vue.$http({
    url: prefix + 'deleteFunctionLibrary',
    method: 'get',
    params
  });
};

/**
 * 更新函数库
 * request: {
 *  "libraryName":"", //函数库名
 *  "desc":"", //说明
 * }
 */
export const updateFunctionLibrary = (data) => {
  return Vue.$http({
    url: prefix + 'updateFunctionLibrary',
    method: 'post',
    data
  });
};

/**
 * 开放函数库
 * request: {
 *  "libraryName":"", //函数库名称
 * }
 */
export const applyFunctionLibrary = (data) => {
  return Vue.$http({
    url: prefix + 'publishFunctionLibraryApply',
    method: 'post',
    data: {
      libraryName: data
    }
  });
};

/**
 * 获取函数库全部函数
 * request: libraryName, cookie //为分页做准备
 */
export const getFunctions = (params) => {
  return Vue.$http({
    url: prefix + 'getFunctions',
    method: 'get',
    params
  });
};

/**
 * 新建函数
 * request: {
 *  "codeBucketName":"", //阿里云Bucket名称
 *  "functionName...":"", //函数名（项目ID）
 *  "ossObjName":"", //文件在OSS的路径
 *  "codeHandler":"", //函数入口
 *  "runTime":"python3", //默认先都填这个
 *  "serviceName...":"", //函数库名
 *  "timeOut": 5, //默认先都填这个
 *  "filePathJson...":"", // 子目录结构
 *  "usePublicLayers": "" //是否使用公共的依赖层
 * }
 */
export const createFunction = (functions) => {
  return Vue.$http({
    url: prefix + 'createFunction',
    method: 'post',
    data: {
      // codeBucketName: bucket_name,
      // ossObjName: `${libraryFolderName}/${functions.libraryName}/${functions.functionName}/${functions.functionName}.zip`,
      // codeHandler: 'index.handler',
      // runTime: 'python3',
	  // memorySize: functions.memorySize,
	  functionName: functions.functionName,
      serviceName: functions.libraryName,
      timeOut: functions.timeOut,
      filePathJson: functions.filePathJson,
      usePublicLayer: functions.usePublicLayers
    }
  });
};

/**
 * 删除函数
 * request: libraryName:"" //函数库名 , functionName:"" // 函数名
 */
export const delFunction = (params) => {
  return Vue.$http({
    url: prefix + 'delFunction',
    method: 'get',
    params
  });
};

/**
 * 更新函数库
 * request: {
 *  "codeBucketName":"", //阿里云Bucket名称
 *  "functionName":"", //函数名（项目ID）
 *  "ossObjName":"", //文件在OSS的路径
 *  "codeHandler":"", //函数入口
 *  "runTime":"python3", //默认先都填这个
 *  "serviceName":"", //函数库名
 *  "timeOut": 5, //默认先都填这个
 *  "filePathJson":"", // 子目录结构
 * }
 */
/**
 * 更新函数
 * @param payload
 * @param{String} payload.libraryName
 * @param{String} payload.functionName
 * @param{String} payload.usePublicLayer
 * @param{String} payload.filePathJson
 *  @param{String} payload.timeOut // 可能没有这个参数
 *  @param{String} payload.memorySize // 可能没有这个参数
 *
 * @return {AxiosPromise}
 */

/*
 * 更新函数文件
*/
export const updateFunctionFiles = (payload) => {
  // 如果data有 timeout memorySize 字段
  const functionOption = {};
  if (payload.timeOut) {
    functionOption.timeOut = payload.timeOut;
  }
  if (payload.memorySize) {
    functionOption.memorySize = payload.memorySize;
  }
  console.log(functionOption);

  return Vue.$http({
    url: prefix + 'updateFunctionFiles',
    method: 'post',
    data: {
      ...functionOption,
      functionName: payload.functionName,
      serviceName: payload.libraryName,
      filePathJson: payload.filePathJson,
    }
  });
};

/*
 * 更新函数配置
*/
export const updateFunctionConfig = (payload) => {
  // 如果data有 timeout memorySize 字段
  const functionOption = {};
  if (payload.timeOut) {
    functionOption.timeOut = payload.timeOut;
  }
  if (payload.memorySize) {
    functionOption.memorySize = payload.memorySize;
  }
  console.log(functionOption);

  return Vue.$http({
    url: prefix + 'updateFunctionConfig',
    method: 'post',
    data: {
      ...functionOption,
      functionName: payload.functionName,
      serviceName: payload.libraryName,
	  timeOut: functions.timeOut,
	  memorySize: functions.memorySize,
      usePublicLayers: payload.usePublicLayers
    }
  });
};


/*
 * 给工程添加层
 * projectName + requirements
 */
export const createLayerForLibrary = (serviceName, functionName, requirements) => {
  return Vue.$http({
    url: prefix + 'layer/library',
    method: 'post',
    data: {
      'serviceName': serviceName,
      'functionName': functionName,
      'requirements': requirements
    }
  });
};

/*
 * 获取工程中层的依赖
 * projectName
 */
export const getLayerDependencyForLibrary = (serviceName, functionName) => {
  return Vue.$http({
    url: prefix + 'layer/library/dependency',
    method: 'get',
    params: {
      serviceName: serviceName,
      functionName: functionName
    }
  });
};
/*
 * 得到对应的公共层信息
*/
export const getPublicLayerForLibrary = () => {
  return Vue.$http({
    url: prefix + 'layer/publicLayer',
    method: 'post',
    data: {
      'name': ''
    }
  });
};
